﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;
using System.Globalization;
using System.Resources;

using EHTWebsite.Code;
using System.Text;

public partial class Site : System.Web.UI.MasterPage
{
    private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
    public Boolean IsVN = true;

    protected void Page_Load(object sender, EventArgs e)
    {

        rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));

        if (!IsPostBack)
        {
            String languageCode = "vi-VN";
            try
            {
                languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
            }
            catch (Exception ex)
            {
            }

            //Load thông tin công ty
            ThongTinCongTy theThongTinCongTy = ThongTinCongTyProvider.GetThongTinCongTy();

            if (theThongTinCongTy != null)
            {

                //this.ltrEmail.Text = theThongTinCongTy.Email;
                //this.ltrHotLine.Text = theThongTinCongTy.DiDong;
                //this.ltrPhone.Text = theThongTinCongTy.DienThoai;
                //this.ltrWebsite.Text = "<a href='" + theThongTinCongTy.Website + "' style='color: Red; text-decoration: underline'>" + theThongTinCongTy.Website + "</a>";

                if (languageCode == "vi-VN")
                {
                    IsVN = true;
                   this.ltrFooter.Text = theThongTinCongTy.FooterVN;
                }
                else
                {
                    IsVN = false;
                    this.ltrFooter.Text = theThongTinCongTy.FooterOther;
                }
            }

            Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
            LoadLanguage(Thread.CurrentThread.CurrentCulture);
        }

        CreateTopMenu();
        CreateFooter();
    }

    protected void ibtnVN_Click(object sender, ImageClickEventArgs e)
    {
        Session[HangSo.LANGUAGE_CODE] = "vi-VN";
        Thread.CurrentThread.CurrentCulture = new CultureInfo(Session[HangSo.LANGUAGE_CODE].ToString());
        String url = Request.Url.AbsoluteUri.ToString().Split('?')[0];
        Response.Redirect(url);
    }

    protected void ibtnEN_Click(object sender, ImageClickEventArgs e)
    {
        Session[HangSo.LANGUAGE_CODE] = "en-us";
        Thread.CurrentThread.CurrentCulture = new CultureInfo(Session[HangSo.LANGUAGE_CODE].ToString());
        String url = Request.Url.AbsoluteUri.ToString().Split('?')[0];
        Response.Redirect(url);
    }

    public void LoadLanguage(CultureInfo ci)
    {
        //this.ltrMenuTop.Text = rm.GetString("ltrMenuTop", ci);
        //this.ltrltrCreateBy.Text = rm.GetString("ltrCreateBy", ci);
        //this.ltrDiaChi_Title.Text = rm.GetString("ltrDiaChi_Title", ci);
    }

    private String CreateNewsList(Int32 danhMucTinTucID)
    {
        StringBuilder sb = new StringBuilder();
        Int32 nhomDanhMucMoRong = 2;
        Int32 totalRecord = 0;
        DataSet dsTinTucs = TinTucProvider.GetTinTucsByFillterForFrontEnd("", "", danhMucTinTucID, 1, 10000, out totalRecord);

        if (dsTinTucs.Tables[0].Rows.Count > 0)
        {
            Int32 totalOfTinTucs = dsTinTucs.Tables[0].Rows.Count;

            for (Int32 i = 0; i < totalOfTinTucs; ++i)
            {
                Int32 tinTucID = Int32.Parse(dsTinTucs.Tables[0].Rows[i]["TinTucID"].ToString());
                String tieuDe = IsVN == true ? dsTinTucs.Tables[0].Rows[i]["TieuDeVN"].ToString() : dsTinTucs.Tables[0].Rows[i]["TieuDeOther"].ToString();

                sb.Append("<ul>");
                sb.Append("<li>");
                sb.Append("<a href='/tin-tuc-chi-tiet/" + tinTucID.ToString() + "/" + FormatStringForUrl(tieuDe) + ".aspx'>");
                sb.Append(tieuDe);
                sb.Append("</a>");
                sb.Append("</li>");
                sb.Append("</ul>");
            }
        }

        return sb.ToString();
    }

    public void CreateFooter()
    {
        Int32 nhomDanhMucMoRong = 2;
        DataSet dsDanhMucTinTucMoRong = DanhMucTinTucProvider.GetDanhMucTinTucsByNhomDanhMucID(nhomDanhMucMoRong);
        StringBuilder sb = new StringBuilder();

        if (dsDanhMucTinTucMoRong.Tables[0].Rows.Count > 0)
        {
            Int32 totalOfDanhMucMoRong = dsDanhMucTinTucMoRong.Tables[0].Rows.Count;

            for (Int32 i = 0; i < totalOfDanhMucMoRong; ++i)
            {
                Int32 danhMucTinTucID = Int32.Parse(dsDanhMucTinTucMoRong.Tables[0].Rows[i]["DanhMucTinTucID"].ToString());
                String tenDanhMuc = IsVN == true ? dsDanhMucTinTucMoRong.Tables[0].Rows[i]["TenDanhMucTinTucVN"].ToString() : dsDanhMucTinTucMoRong.Tables[0].Rows[i]["TenDanhMucTinTucOther"].ToString();

                sb.Append("<div id='bao-right' style='float: left;'>");
                sb.Append("<h3>");
                sb.Append(tenDanhMuc);
                sb.Append("</h3>");
                sb.Append(CreateNewsList(danhMucTinTucID));
                sb.Append("</div>");
            }
        }

       // this.ltrFooter.Text = sb.ToString();
    }

    public void CreateTopMenu()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<ul id='jsddm'>");
        sb.Append("<li><a href='/trang-chu.aspx' id='t_1'>" + ((IsVN == true) ? "Trang Chủ" : "Home") + "</a></li>");
        sb.Append("<li><a href='/gioi-thieu.aspx' id='t_2'>" + ((IsVN == true) ? "Giới Thiệu" : "About Us") + "</a></li>");
        sb.Append("<li><a href='#' id='t_3'>" + ((IsVN == true) ? "Dịch Vụ" : "Services") + "</a>");
        sb.Append(CreateDichVu());
        sb.Append("</li>");
        sb.Append("<li><a href='#' id='t_4'>" + ((IsVN == true) ? "Hire Developer" : "Hire Developer") + "</a>");
        sb.Append(CreateHireDeveloper());
        sb.Append("</li>");
        sb.Append("<li><a href='/khach-hang.aspx' id='t_5'>" + ((IsVN == true) ? "Khách Hàng" : "Customers") + "</a>");
        sb.Append("<li><a href='#'>" + ((IsVN == true) ? "Kiến Thức" : "Knowledge") + "</a>");
        sb.Append(CreateKienThuc());
        sb.Append("</li>");
        sb.Append("<li><a href='/dich-vu.aspx' id='t_6'>" + ((IsVN == true) ? "Báo Giá" : "Prices") + "</a>");
        sb.Append("<li><a href='/lien-he.aspx' id='t_7'>" + ((IsVN == true) ? "Liên Hệ" : "Contact") + "</a>");
        sb.Append("</ul>");

        //this.ltrMenuTop.Text = sb.ToString();
    }

    private String CreateHireDeveloper()
    {
        Int32 nhomDanhMucMoRong = 3;
        DataSet dsDanhMucTinTucMoRong = DanhMucTinTucProvider.GetDanhMucTinTucsByNhomDanhMucID(nhomDanhMucMoRong);
        StringBuilder sb = new StringBuilder();

        if (dsDanhMucTinTucMoRong.Tables[0].Rows.Count > 0)
        {
            Int32 totalOfDanhMucMoRong = dsDanhMucTinTucMoRong.Tables[0].Rows.Count;

            sb.Append("<ul class='top-menu-dichvu'>");
            sb.Append(" <li>");
            sb.Append(" <div>");

            for (Int32 i = 0; i < totalOfDanhMucMoRong; ++i)
            {
                Int32 danhMucTinTucID = Int32.Parse(dsDanhMucTinTucMoRong.Tables[0].Rows[i]["DanhMucTinTucID"].ToString());
                sb.Append(CreateHireDeveloperItem(danhMucTinTucID));
            }
            sb.Append(" <div class='clear:both;' />");
            sb.Append(" </div>");
            sb.Append("</li>");
            sb.Append("</ul>");
        }
        return sb.ToString();
    }

    private String CreateDichVu()
    {
        Int32 nhomDanhMucMoRong = 2;
        DataSet dsDanhMucTinTucMoRong = DanhMucTinTucProvider.GetDanhMucTinTucsByNhomDanhMucID(nhomDanhMucMoRong);
        StringBuilder sb = new StringBuilder();

        if (dsDanhMucTinTucMoRong.Tables[0].Rows.Count > 0)
        {
            Int32 totalOfDanhMucMoRong = dsDanhMucTinTucMoRong.Tables[0].Rows.Count;

            sb.Append("<ul class='top-menu-dichvu'>");
            sb.Append(" <li>");
            sb.Append(" <div>");

            for (Int32 i = 0; i < totalOfDanhMucMoRong; ++i)
            {
                Int32 danhMucTinTucID = Int32.Parse(dsDanhMucTinTucMoRong.Tables[0].Rows[i]["DanhMucTinTucID"].ToString());
                sb.Append(CreateDichVuItem(danhMucTinTucID));
            }
            sb.Append(" <div class='clear:both;' />");
            sb.Append(" </div>");
            sb.Append("</li>");
            sb.Append("</ul>");
        }
        return sb.ToString();
    }

    private String CreateDichVuItem(Int32 danhMucTinTucID)
    {
        StringBuilder sb = new StringBuilder();
        Int32 nhomDanhMucMoRong = 2;
        Int32 totalRecord = 0;

        sb.Append("<table width='200px' class='dichvu-item'>");
        DanhMucTinTuc theDanhMucTinTuc = DanhMucTinTucProvider.GetDanhMucTinTucByDanhMucID(danhMucTinTucID);
        sb.Append("<tr style='width:200px;'>");
        sb.Append("<td>");
        sb.Append("<h3>" + (IsVN == true ? theDanhMucTinTuc.TenDanhMucTinTucVN : theDanhMucTinTuc.TenDanhMucTinTucOther) + "</h3>");
        sb.Append("</td>");
        sb.Append("</tr>");
        DataSet dsTinTucs = TinTucProvider.GetTinTucsByFillterForFrontEnd("", "", danhMucTinTucID, 1, 10000, out totalRecord);


        if (dsTinTucs.Tables[0].Rows.Count > 0)
        {
            Int32 totalOfTinTucs = dsTinTucs.Tables[0].Rows.Count;


            for (Int32 i = 0; i < totalOfTinTucs; ++i)
            {
                Int32 tinTucID = Int32.Parse(dsTinTucs.Tables[0].Rows[i]["TinTucID"].ToString());
                String tieuDe = IsVN == true ? CatString(dsTinTucs.Tables[0].Rows[i]["TieuDeVN"].ToString()) : CatString(dsTinTucs.Tables[0].Rows[i]["TieuDeOther"].ToString());
                sb.Append("<tr style='width:200px;'>");
                sb.Append("<td>");
                sb.Append("<a href='/tin-tuc-chi-tiet/" + tinTucID.ToString() + "/" + FormatStringForUrl(tieuDe) + ".aspx'>");
                sb.Append(tieuDe);
                sb.Append("</a>");
                sb.Append("</td>");
                sb.Append("</tr>");
            }
        }
        sb.Append("</table>");
        return sb.ToString();

    }

    private String CreateHireDeveloperItem(Int32 danhMucTinTucID)
    {
        StringBuilder sb = new StringBuilder();
        Int32 nhomDanhMucMoRong = 3;
        Int32 totalRecord = 0;

        sb.Append("<table width='200px' class='dichvu-item'>");
        DanhMucTinTuc theDanhMucTinTuc = DanhMucTinTucProvider.GetDanhMucTinTucByDanhMucID(danhMucTinTucID);
        sb.Append("<tr style='width:200px;'>");
        sb.Append("<td>");
        sb.Append("<h3>" + (IsVN == true ? theDanhMucTinTuc.TenDanhMucTinTucVN : theDanhMucTinTuc.TenDanhMucTinTucOther) + "</h3>");
        sb.Append("</td>");
        sb.Append("</tr>");

        DataSet dsTinTucs = TinTucProvider.GetTinTucsByFillterForFrontEnd("", "", danhMucTinTucID, 1, 10000, out totalRecord);


        if (dsTinTucs.Tables[0].Rows.Count > 0)
        {
            Int32 totalOfTinTucs = dsTinTucs.Tables[0].Rows.Count;


            for (Int32 i = 0; i < totalOfTinTucs; ++i)
            {
                Int32 tinTucID = Int32.Parse(dsTinTucs.Tables[0].Rows[i]["TinTucID"].ToString());
                String tieuDe = IsVN == true ? CatString(dsTinTucs.Tables[0].Rows[i]["TieuDeVN"].ToString()) : CatString(dsTinTucs.Tables[0].Rows[i]["TieuDeOther"].ToString());
                sb.Append("<tr style='width:200px;'>");
                sb.Append("<td>");
                sb.Append("<a href='/tin-tuc-chi-tiet/" + tinTucID.ToString() + "/" + FormatStringForUrl(tieuDe) + ".aspx'>");
                sb.Append(tieuDe);
                sb.Append("</a>");
                sb.Append("</td>");
                sb.Append("</tr>");
            }
        }
        sb.Append("</table>");
        return sb.ToString();

    }

    private String CreateKienThuc()
    {
        StringBuilder sb = new StringBuilder();
        Int32 nhomDanhMucID = 4;
        DataSet dsKienThuc = DanhMucTinTucProvider.GetDanhMucTinTucsByNhomDanhMucID(nhomDanhMucID);
        Int32 amountOfDanhMuc = dsKienThuc.Tables[0].Rows.Count;
        sb.Append("<ul>");

        for (int i = 0; i < amountOfDanhMuc; ++i)
        {
            String tieuDe = (IsVN == true ? CatString(dsKienThuc.Tables[0].Rows[i]["TenDanhMucTinTucVN"].ToString()) : CatString(dsKienThuc.Tables[0].Rows[i]["TenDanhMucTinTucOther"].ToString()));
            sb.Append("<li>");
            sb.Append("<a href='/tin-tuc/" + dsKienThuc.Tables[0].Rows[i]["DanhMucTinTucID"].ToString() + "/" + FormatStringForUrl(tieuDe) + ".aspx'>" + tieuDe + "</a>");
            sb.Append("</li>");
        }
       
        sb.Append("</ul>");
        return sb.ToString();
    }

    public String FormatStringForUrl(String input)
    {
        return HelpMethod.FormatStringForUrl(input);
    }
    private String CatString(String input)
    {
        return HelpMethod.CatString(input, 22);
    }

}
